import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import {store} from '../store/index'
import checkToken from './checkToken'
	
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Login',
    component: ()=>import('../views/Login/index.vue')
  },
	{
	  path: '/404',
	  name: '404',
	  component: ()=>import('./404.vue')
	},
	{
	  path: '/test',
	  name: 'test',
	  component: ()=>import('./test.vue')
	},
	{
		path:'/singup',
		name:'singup',
		component:()=>import('../views/Login/singup.vue')
	},
	{
		path:'/Search',
		component:()=>import('../Pages/Search/index.vue')
	},
	{
		path:'/Frame',
		name:'Frame',
		component:()=>import('../views/Frame/index.vue'),
		children:[
			{
				path:'Home',
				name:'Home',
				component:()=>import('../views/Home/index.vue'),
			},
			{
				path:'Carts',
				name:'Carts',
				component:()=>import('../views/Carts/index.vue'),
			},
			{
				path:'Message',
				name:'Message',
				component:()=>import('../views/Message/index.vue'),
			},
			{
				path:'Me',
				name:'Me',
				component:()=>import('../views/Me/index.vue'),
			},
		]
	}
]
export const router:any= createRouter({
  history: createWebHashHistory(),
  routes
})
//拦截陆游
router.beforeEach(async (to:any,from:any,next:any)=>{
	//过滤不检查 token 的陆游
	if(to.path=='/' || to.path=='/singup' || to.path=='/404'){
	
		next();
		return;
	}
	
	var token:string=localStorage.getItem('token')||'';
	var result:any=await checkToken(token);
	if(!result)result=await checkToken(store.getters.getToken);
	if(result){
		next();
	}else{
		next({
			path:'/'
		});
	}
})
export const Store:any=store;


